{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "969a06f5-19ec-45fb-b68a-73750a7a61f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "90dc903b-66f1-4cca-8279-3277866f6a6e",
   "metadata": {},
   "outputs": [],
   "source": [
    "address = \"乐其国际影城\"\n",
    "city = \"天津\"\n",
    "address = urllib.parse.quote(address)\n",
    "city = urllib.parse.quote(city)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "68d1c851-03ec-435b-ad47-76b7fe6ae785",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'%E4%B9%90%E5%85%B6%E5%9B%BD%E9%99%85%E5%BD%B1%E5%9F%8E'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "'%E5%A4%A9%E6%B4%A5'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "address\n",
    "city"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "c9bed42b-bbfe-4fa6-bfe1-bde1a80a63b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://restapi.amap.com/v3/geocode/geo?key=947c7f60f731b54bd6474ce18d5d894b&address={}&city={}\".format(address,city)\n",
    "req = urllib.request.Request(url)  # 将url对象转化为request对象\n",
    "resp = urllib.request.urlopen(req)  # 发送请求，打开url，也可以直接传入url\n",
    "data = resp.read().decode('utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f49dc04b-ae08-4750-8d4e-7ae2d7b7a397",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"status\":\"0\",\"info\":\"ENGINE_RESPONSE_DATA_ERROR\",\"infocode\":\"30001\"}'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "str"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data\n",
    "type(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "99ed783e-5d09-45d8-8c6c-5a2c046cd386",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "jso = json.loads(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a1fe2ad8-012a-4e03-bb6f-e2d6394a674e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': '0', 'info': 'ENGINE_RESPONSE_DATA_ERROR', 'infocode': '30001'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "jso"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2314206f-7279-409e-b3da-03fa4b935b60",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(jso['status'] == '0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bc3a3807-1352-486e-abc2-665ac072e085",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'geocodes'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_12452\\885876943.py\u001b[0m in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mjso\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'geocodes'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'geocodes'"
     ]
    }
   ],
   "source": [
    "jso['geocodes'][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a8d2f35-0a65-4140-9ddc-167eb6464948",
   "metadata": {},
   "outputs": [],
   "source": [
    "type(jso)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "64f6d9a2-e0dd-4fb7-9f6a-bedc7a4e7b4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = eval(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e3da6995-7a60-43e5-a380-f40215bb2240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': '1',\n",
       " 'info': 'OK',\n",
       " 'infocode': '10000',\n",
       " 'count': '1',\n",
       " 'geocodes': [{'formatted_address': '天津市南开区南开大学',\n",
       "   'country': '中国',\n",
       "   'province': '天津市',\n",
       "   'citycode': '022',\n",
       "   'city': '天津市',\n",
       "   'district': '南开区',\n",
       "   'township': [],\n",
       "   'neighborhood': {'name': [], 'type': []},\n",
       "   'building': {'name': [], 'type': []},\n",
       "   'adcode': '120104',\n",
       "   'street': [],\n",
       "   'number': [],\n",
       "   'location': '117.169990,39.103958',\n",
       "   'level': '兴趣点'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6ad553dc-ad8e-4328-a9b1-aab312e41389",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'117.169990,39.103958'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t['geocodes'][0]['location']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "54a1c8f3-3836-49a1-abc3-b56ca9542796",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c44f40cb-11d7-4e8d-9833-6e7622bce825",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(114, 112)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eval('114,112')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "213190df-b762-43bd-903f-d0dd00d3d774",
   "metadata": {},
   "outputs": [],
   "source": [
    "regeo_url = 'https://restapi.amap.com/v3/geocode/regeo?location={}&key=947c7f60f731b54bd6474ce18d5d894b&extensions=base'.format(t['geocodes'][0]['location'])\n",
    "req = urllib.request.Request(regeo_url)  # 将url对象转化为request对象\n",
    "resp = urllib.request.urlopen(req)  # 发送请求，打开url，也可以直接传入url\n",
    "data = resp.read().decode('utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "29d85246-c215-46e4-8691-5345429e78b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': '1',\n",
       " 'regeocode': {'addressComponent': {'city': [],\n",
       "   'province': '天津市',\n",
       "   'adcode': '120104',\n",
       "   'district': '南开区',\n",
       "   'towncode': '120104006000',\n",
       "   'streetNumber': {'number': '18号',\n",
       "    'location': '117.171147,39.103955',\n",
       "    'direction': '东',\n",
       "    'distance': '99.8351',\n",
       "    'street': '复康路'},\n",
       "   'country': '中国',\n",
       "   'township': '学府街道',\n",
       "   'businessAreas': [{'location': '117.195938,39.101170',\n",
       "     'name': '马场道',\n",
       "     'id': '120103'}],\n",
       "   'building': {'name': [], 'type': []},\n",
       "   'neighborhood': {'name': '南开大学', 'type': '科教文化服务;学校;高等院校'},\n",
       "   'citycode': '022'},\n",
       "  'formatted_address': '天津市南开区学府街道南开大学南开大学八里台校区'},\n",
       " 'info': 'OK',\n",
       " 'infocode': '10000'}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = eval(data)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3df8b710-6396-4a38-9172-133ac4446c8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'南开区'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "district = data['regeocode']['addressComponent']['district']\n",
    "district"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "8d035110-a3a3-4446-add7-c742a1d7f7fd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'复康路'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "street = data['regeocode']['addressComponent']['streetNumber']['street']\n",
    "street"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "718ff820-bf87-4fc3-bdb0-fd769c82e88e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'学府街道'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "township = data['regeocode']['addressComponent']['township']\n",
    "township"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "573713ff-9e51-4d55-8091-559fa725f38f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'天津市南开区学府街道南开大学南开大学八里台校区'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "formatted_address = data['regeocode']['formatted_address']\n",
    "formatted_address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "f554b0e4-04e0-4fc6-8f16-9308e550b576",
   "metadata": {},
   "outputs": [],
   "source": [
    "t = pd.DataFrame([[1,2],[2,3]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "9eb17847-88f9-4857-8469-92050546d450",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1\n",
       "0  1  2\n",
       "1  2  3"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6280439e-07f5-4c7c-9c08-d04a25048042",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
